package com.costeaalex.MIDlets.remote.bluetooth;

import java.util.Vector;

import javax.bluetooth.DeviceClass;
import javax.bluetooth.DiscoveryAgent;
import javax.bluetooth.DiscoveryListener;
import javax.bluetooth.LocalDevice;
import javax.bluetooth.RemoteDevice;
import javax.bluetooth.ServiceRecord;
import javax.bluetooth.UUID;

import com.costeaalex.MIDlets.remote.BlueMIDlet;

public class DeviceFinder implements DiscoveryListener
	{
	public Vector				btDevicesFound;
	private BlueMIDlet			midlet;
	private LocalDevice local = null;
	private DiscoveryAgent discoveryAgent = null;
	
	public DeviceFinder(BlueMIDlet midl)
		{
		btDevicesFound = new Vector();
		midlet = midl;
		}

	public int find(UUID[] aServices)
		{
		findDevices();
		return btDevicesFound.size();
		}

	public int findDevices()
		{
		try
			{
			btDevicesFound.removeAllElements();
			local = LocalDevice.getLocalDevice();
			discoveryAgent = local.getDiscoveryAgent();
			discoveryAgent.startInquiry(DiscoveryAgent.GIAC, this);
			}
		catch (Exception e)
			{
			e.printStackTrace();
			}
		return btDevicesFound.size();
		}

	public void deviceDiscovered(RemoteDevice remoteDevice, DeviceClass deviceClass)
		{
		btDevicesFound.addElement(remoteDevice);
		}

	public void inquiryCompleted(int param)
		{
		midlet.devicesFound(btDevicesFound);
		}
	
	public String getDeviceName(int deviceID)
		{
		try
			{
			return ((RemoteDevice) btDevicesFound.elementAt(deviceID)).getFriendlyName(false);
			}
		catch (Exception e)
			{
			e.printStackTrace();
			}
		return "Error";
		}

	public void serviceSearchCompleted(int arg0, int arg1)
		{
		// TODO Auto-generated method stub

		}

	public void servicesDiscovered(int arg0, ServiceRecord[] arg1)
		{
		// TODO Auto-generated method stub

		}

	}